package hz.mobileproject.pda.ui.activity.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;
import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.request.Request_delete_oddPost_list;
import hz.mobileproject.pda.entity.request.Request_oddpost_query;
import hz.mobileproject.pda.entity.response.OddPostListBean;
import hz.mobileproject.pda.presenter.POddPost;
import hz.mobileproject.pda.ui.activity.AddOddPostActivity;
import hz.mobileproject.pda.ui.adapter.oddPostItemAdapter;

/**
 * 零星报备Activity
 */
public class OddPostActivity extends XActivity<POddPost> {


    ImageView mIvAdd;
    ImageView mIvSearchTime;
    RecyclerView mRecyclerviewAddPost;
    SmartRefreshLayout mSmartAddPost;

    private List<OddPostListBean> mOddPostListBeanList;

    private int page = 1;



    private SPUtils spUtils;
    private Gson gson;

    private int deletePosition = -1;
    private oddPostItemAdapter mOddPostItemAdapter;
    private String mCus_number;
    private String mDept_id;
    private String mTime;
    private String select_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public void onWidgetClick(View view) {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        spUtils = SPUtils.getInstance("app_login");
        gson = new Gson();
        mIvAdd = (ImageView) findViewById(R.id.iv_add);
        mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(OddPostActivity.this,AddOddPostActivity.class));
            }
        });
        mIvSearchTime = (ImageView) findViewById(R.id.iv_search_time);
        mIvSearchTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //选择月份查询按钮点击
                DatePickDialog dialog = new DatePickDialog(OddPostActivity.this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //回显当前选择时间
                if(!StringUtils.isEmpty(select_time)){
                    try {
                        dialog.setStartDate(new SimpleDateFormat("yyyy-MM").parse(select_time));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        LogUtils.e(date);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                        select_time = sdf.format(date);

                        //点击选择时间后 重新请求网络
                        Request_oddpost_query request_oddpost_query_search = new Request_oddpost_query(mCus_number, mDept_id, select_time,page + "");
                        LogUtils.e("select",request_oddpost_query_search);
                        Gson gson = new Gson();
                        String selectJson = gson.toJson(request_oddpost_query_search);
                        getP().setData(selectJson,1);

                    }
                });
                dialog.show();

            }
        });
        mRecyclerviewAddPost  = (RecyclerView) findViewById(R.id.recyclerview_add_post);
        mSmartAddPost = (SmartRefreshLayout) findViewById(R.id.smart_add_post);


    }


    @Override
    protected void onResume() {
        super.onResume();
        mCus_number = spUtils.getString("cusnumber");
        mDept_id = spUtils.getString("department_id");
        mTime = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM"));

        Request_oddpost_query request_oddpost_query = new Request_oddpost_query(mCus_number, mDept_id, mTime,page + "");
        getP().setData(gson.toJson(request_oddpost_query),1);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_odd_post;
    }

    @Override
    public POddPost newP() {
        return new POddPost();
    }

    public void setView(String json, int type) {
        switch (type) {
            case 1: {
                LogUtils.e(json);
                mOddPostListBeanList = gson.fromJson(json,new TypeToken<List<OddPostListBean>>(){}.getType());

                mRecyclerviewAddPost.setLayoutManager(new LinearLayoutManager(OddPostActivity.this));
                //ToastUtils.showLong(position + "");
                //点击了删除按钮
                mOddPostItemAdapter = new oddPostItemAdapter(mOddPostListBeanList, new oddPostItemAdapter.DelCallback() {
                    @Override
                    public void click(View v, int position, List<OddPostListBean> stringList) {
//                        ToastUtils.showLong(position + "");
                        deletePosition = position;
                        LogUtils.e("删除1",deletePosition);
                        //点击了删除按钮
                        Request_delete_oddPost_list request_delete_oddPost_list = new Request_delete_oddPost_list(
                                stringList.get(position).id,stringList.get(position).report_dept_id

                        );
                        LogUtils.e(gson.toJson(request_delete_oddPost_list));

                        getP().setData(gson.toJson(request_delete_oddPost_list),2);

                    }
                });
                mRecyclerviewAddPost.setAdapter(mOddPostItemAdapter);

                mSmartAddPost.setOnLoadmoreListener(new OnLoadmoreListener() {
                    @Override
                    public void onLoadmore(RefreshLayout refreshlayout) {
                        page = page + 1;
//                        ToastUtils.showLong(page + "");
                        Request_oddpost_query request_oddpost_query = new Request_oddpost_query(mCus_number, mDept_id, mTime,page + "");
                        getP().setData(gson.toJson(request_oddpost_query),3);   //加载更多

                    }
                });
            }
            break;
            case 2: {
                //传过来的是rtnCode
                LogUtils.e("删除的",json);
                ToastUtils.showLong("删除成功");
                mOddPostListBeanList.remove(deletePosition);
                LogUtils.e("删除2",deletePosition);
                mOddPostItemAdapter.notifyDataSetChanged();

            }
            break;
            case 3 : {
                //TODO 加载更多的回调方法
                List<OddPostListBean> loadMoreOddPostList = gson.fromJson(json,new TypeToken<List<OddPostListBean>>(){}.getType());
                mOddPostListBeanList.addAll(loadMoreOddPostList);
                mOddPostItemAdapter.notifyDataSetChanged();
                mSmartAddPost.finishLoadmore();
            }
            break;
        }
    }
}
